package com.heking.securityapp.pages;

import android.os.Bundle;
import android.util.SparseArray;
import android.widget.Button;
import android.widget.TextView;

import com.heking.security.BundleWrap;
import com.heking.security.LogUtils;
import com.heking.securityapp.R;
import com.heking.securityapp.entiry.ParcelableA;
import com.heking.securityapp.entiry.ParcelableB;
import com.heking.securityapp.entiry.ParcelableParent;
import com.heking.securityapp.entiry.SerializableA;
import com.heking.securityapp.entiry.SerializableB;

import java.util.ArrayList;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

/**
 * @Author 35574
 * @Date 2021/10/25
 * @Description
 */
public class BundleWrapActivity extends AppCompatActivity {
    private static final String TAG = ">>>BundleWrapActivity";
    private TextView mTvTitle;
    private Button mBtn1;
    private Button mBtn2;
    private Button mBtn3;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.bundle_wrap_activity);
        findViews();
        addListener();
    }

    private void findViews() {
        mTvTitle = findViewById(R.id.tv_title);
        mBtn1 = findViewById(R.id.btn1);
        mBtn2 = findViewById(R.id.btn2);
        mBtn3 = findViewById(R.id.btn3);
    }

    private void addListener() {
        mBtn1.setOnClickListener(v -> {
            Bundle bundle = new Bundle();
            bundle.putSerializable("a", new SerializableA("nameA", 11));
            bundle.putSerializable("b", new SerializableB("nameB", 22));
            BundleWrap bundleWrap = BundleWrap.with(bundle);
            SerializableA a = bundleWrap.getSerializable("a", SerializableA.class);
            SerializableA b = bundleWrap.getSerializable("b", SerializableA.class);
            LogUtils.d(TAG, "a=" + a);
            LogUtils.d(TAG, "b=" + b);


        });
        mBtn2.setOnClickListener(v -> {
            Bundle bundle = new Bundle();
            bundle.putParcelable("a", new ParcelableA("nameA", 33));
            bundle.putParcelable("b", new ParcelableB("nameB", 44));
            ArrayList<ParcelableA> parcelableParents = new ArrayList<>();
            parcelableParents.add(new ParcelableA("name1", 100));
            parcelableParents.add(new ParcelableA("name2", 200));
            bundle.putParcelableArrayList("parcelableParents", parcelableParents);

            BundleWrap bundleWrap = BundleWrap.with(bundle);
            ParcelableA a = bundleWrap.getParcelable("a", ParcelableA.class);
            ParcelableA b = bundleWrap.getParcelable("b", ParcelableA.class);

            ArrayList<ParcelableParent> parcelableArrayList1 = bundleWrap.getParcelableArrayList("parcelableParents", ParcelableParent.class);

            LogUtils.d(TAG, "a=" + a);
            LogUtils.d(TAG, "b=" + b);
            if (parcelableArrayList1 != null) {
                for (int i = 0; i < parcelableArrayList1.size(); i++) {
                    ParcelableParent parcelableB = parcelableArrayList1.get(i);
                    LogUtils.d(TAG, "parcelableArrayList1=" + parcelableB);
                }
            }

        });
        mBtn3.setOnClickListener(v -> {
            Bundle bundle = new Bundle();

            SparseArray<ParcelableA> sparseArray = new SparseArray<>();
            sparseArray.put(10, new ParcelableA("name1", 100));
            sparseArray.put(20, new ParcelableA("name2", 200));
            bundle.putSparseParcelableArray("sparseArray", sparseArray);

            BundleWrap bundleWrap = BundleWrap.with(bundle);
            SparseArray<ParcelableA> list = bundleWrap.getSparseParcelableArray("sparseArray",ParcelableA.class);
            if (list != null && list.size() > 0) {
                for (int i = 0; i < list.size(); i++) {
                    int key = list.keyAt(i);
//                    Logger.d(TAG, key+":" + list.get(key));
                    ParcelableA item = list.get(key);
                    LogUtils.d(TAG, key+":" + item);
                }
            }
        });
    }


}
